package cn.krui.project.controller.article;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.web.bind.annotation.RestController;
import cn.krui.helper.bean.AppResult;
import cn.krui.helper.bean.DataArray;
import cn.krui.project.controller.ArticleBaseController;
import cn.krui.project.bean.article.Article;
import cn.krui.project.bean.article.ArticleView;
import cn.krui.project.service.article.ArticleService;
import java.util.List;
import javax.validation.Valid;
import org.springframework.validation.BindingResult;
import org.springframework.context.support.DefaultMessageSourceResolvable;
import org.springframework.validation.ObjectError;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import cn.krui.helper.tools.DateTimeUtils;
/**
 * @author: zc
 * email: zc8023zs@163.com
 * create_time: 2020/6/11 16:41:40
 */
@Api(tags = "article", description = "内容管理")
@RestController
@RequestMapping("/article/article")
public class ArticleController extends ArticleBaseController {

    @Autowired
    private ArticleService articleService;

    @PostMapping(value = "/add")
	@ApiOperation(value = "内容管理添加", httpMethod = "POST")
    public AppResult<Integer> add(@Valid Article model,BindingResult bindingResult){
        AppResult<Integer> app = new AppResult<Integer>();
        if (bindingResult.hasErrors()) {
            List<ObjectError> oerr = bindingResult.getAllErrors();
            for (ObjectError objectError : oerr) {
                DefaultMessageSourceResolvable d = (DefaultMessageSourceResolvable)objectError.getArguments()[0];
                String columnname = d.getDefaultMessage();
                //主键不用判断
                if("id".equals(columnname)){
					continue;
				}
                //版本字段不用判断
                if("version_id".equals(columnname)){
					continue;
				}
                app.no(bindingResult.getAllErrors().get(0).getDefaultMessage());
                return app;
            }
        }
        Integer i = articleService.add(model);
        if(i>0){
			i = model.getId();
		}
        app.setDataSingle(i);
        app.ok("添加成功");
        return app;
    }

    @PostMapping(value = "/update")
	@ApiOperation(value = "内容管理修改", httpMethod = "POST")
    public AppResult<Integer> update(@Valid Article model,BindingResult bindingResult){
        AppResult<Integer> app = new AppResult<Integer>();
        if (bindingResult.hasErrors()) {
            List<ObjectError> oerr = bindingResult.getAllErrors();
            for (ObjectError objectError : oerr) {
                DefaultMessageSourceResolvable d = (DefaultMessageSourceResolvable)objectError.getArguments()[0];
                String columnname = d.getDefaultMessage();
                //版本字段不用判断
                if("version_id".equals(columnname)){
					continue;
				}
                app.no(bindingResult.getAllErrors().get(0).getDefaultMessage());
                return app;
            }
        }
        Integer i = articleService.update(model);
        app.ok("修改成功");
        app.setDataSingle(i);
        return app;
    }

    @PostMapping(value = "/del")
	@ApiOperation(value = "内容管理删除", httpMethod = "POST")
    public AppResult<Integer> del(Integer id){
        AppResult<Integer> app = new AppResult<Integer>();
        Integer i = articleService.del(id);
        app.ok("删除成功");
        app.setDataSingle(i);
        return app;
    }

	/**
	 * @JSON(type =ArticleView.class, include = "") //只支持简单对象序列化过滤，自行添加需要输出的字段
	 */
    @GetMapping(value = "/info")
	@ApiOperation(value = "内容管理详情", httpMethod = "GET")
    public AppResult<ArticleView> info(Integer id){
        AppResult<ArticleView> app = new AppResult<ArticleView>();
        ArticleView i = articleService.info(id);
        app.setDataSingle(i);
        app.ok("查询成功");
        return app;
    }

	/**
	 * @JSON(type =ArticleView.class, include = "") //只支持简单对象序列化过滤，自行添加需要输出的字段
	 */
    @PostMapping(value = "/select")
	@ApiOperation(value = "内容管理列表", httpMethod = "POST") 
    public AppResult<ArticleView> select(
            @ApiParam(value = "栏目id", required = false) @RequestParam(value = "class_id", required = false) Integer class_id,
            @ApiParam(value = "标题", required = false) @RequestParam(value = "title", required = false) String title,
            @ApiParam(value = "内容模型", required = false) @RequestParam(value = "model_type", required = false) Integer model_type,
            @ApiParam(value = "状态", required = false) @RequestParam(value = "status", required = false) Integer status,
            @ApiParam(value = "创建时间", required = false) @RequestParam(value = "create_times", required = false) String[] create_times
        ){
        AppResult<ArticleView> app = new AppResult<ArticleView>();
        QueryWrapper<ArticleView> where = new QueryWrapper<ArticleView>();
        if(class_id!=null){
			where.eq("class_id", class_id);
		}
        if(title!=null){
			where.eq("title", title);
		}
        if(model_type!=null){
			where.eq("model_type", model_type);
		}
        if(status!=null){
			where.eq("status", status);
		}
        if(create_times!=null){
            try {
                where.between("create_time", create_times[0], new DateTimeUtils(create_times[1],"yyyy/MM/dd").addDays(1).toDate());
            } catch (Exception e) {
            }
        }
        List<ArticleView> i = articleService.select(where);
        DataArray<ArticleView> da=new DataArray<ArticleView>();
        da.setDataList(i);
        app.setDataArray(da);
        app.ok("查询成功");
        return app;
    }

	/**
	 * @JSON(type =ArticleView.class, include = "") //只支持简单对象序列化过滤，自行添加需要输出的字段
	 */
    @PostMapping(value = "/selectpage")
    @ApiOperation(value = "内容管理列表分页", httpMethod = "POST")
    public AppResult<ArticleView> selectpage(
            @ApiParam(value = "栏目id", required = false) @RequestParam(value = "class_id", required = false) Integer class_id,
            @ApiParam(value = "标题", required = false) @RequestParam(value = "title", required = false) String title,
            @ApiParam(value = "内容模型", required = false) @RequestParam(value = "model_type", required = false) Integer model_type,
            @ApiParam(value = "状态", required = false) @RequestParam(value = "status", required = false) Integer status,
            @ApiParam(value = "创建时间", required = false) @RequestParam(value = "create_times", required = false) String[] create_times,
            @ApiParam(value = "页", required = true) @RequestParam(value = "page", required = false, defaultValue = "1") Integer page,
            @ApiParam(value = "条", required = true) @RequestParam(value = "size", required = false, defaultValue = "20") Integer size) {
        AppResult<ArticleView> app = new AppResult<ArticleView>();
        QueryWrapper<ArticleView> where = new QueryWrapper<ArticleView>();
        if(class_id!=null){
			where.eq("class_id", class_id);
		}
        if(title!=null && title!=""){
			where.like("title", title);
		}
        if(model_type!=null){
			where.eq("model_type", model_type);
		}
        if(status!=null){
			where.eq("status", status);
		}
        if(create_times!=null){
            try {
                where.between("create_time", create_times[0], new DateTimeUtils(create_times[1],"yyyy/MM/dd").addDays(1).toDate());
            } catch (Exception e) {
            }
        }
        IPage<ArticleView> pg = new Page<ArticleView>();
        pg.setCurrent(page); 
        pg.setSize(size);
        IPage<ArticleView> i = articleService.select(pg,where);
        int total = articleService.selectCount(where);
        DataArray<ArticleView> da = new DataArray<ArticleView>();
        da.setPageCount((total/size)+1);
        da.setDataCount(total);
        da.setPageIndex(page);
        da.setPageSize(size);
        da.setDataList(i.getRecords());
        app.setDataArray(da);
        app.ok("查询成功");
        return app;
    }

}